<script setup lang='ts'>
import { loginRules } from "./utils/rule";
import { Lock, User } from "@element-plus/icons-vue";
import type { FormInstance } from "element-plus";
import { useRouter } from "vue-router";
import { reactive, ref } from "vue";
import { loginAPI, permissionApi } from "@/api/login";
import { ElMessage } from 'element-plus'
defineOptions({
    name: 'login'
})
//定义接口
interface RuleForm {
    account: string;
    password: string;
    Department?: string;
}
const router = useRouter();
const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
const ruleForm = reactive<RuleForm>({
    account: "u133",
    password: "123456"
});
const submitForm = async (formEl: FormInstance | undefined) => {

    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            getlogin(ruleForm)
        } else {
            console.log("表单校验不通过!", fields);
        }
    });
};
const getlogin = async (ruleForm: RuleForm) => {
    const res: any = await loginAPI(ruleForm)
    if (res.data) {
        localStorage.setItem("token", res.data)
        router.push("/Home")
        ElMessage({
            message: '登录成功',
            type: 'success',
        })
    }
    const permission: any = await permissionApi()
    const data = JSON.stringify(permission.data)
    localStorage.setItem("permission",data)
}

</script>

<template>
    <div id="login">
        <div class="login_box">
            <div class="left"></div>
            <div class="right">
                <div class="login_title">登陆</div>
                <el-form ref="ruleFormRef" :rules="loginRules" :model="ruleForm" label-position="top" label-width="auto"
                    :size="formSize" status-icon>
                    <el-form-item label="账号" prop="username">
                        <el-input :prefix-icon="User" v-model="ruleForm.account" />
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input :prefix-icon="Lock" v-model="ruleForm.password" />
                    </el-form-item>

                    <!-- <el-form-item label="科室">
                        <el-col :span="11">
                            <el-select v-model="ruleForm.Department" placeholder="">
                                <el-option label="选项一" value="shanghai" />
                                <el-option label="选项二" value="beijing" />
                            </el-select>
                        </el-col>
                        <el-col :span="2" class="text-center">
                            <span class="text-gray-500"></span>
                        </el-col>
                        <el-col :span="11">
                            <el-select v-model="ruleForm.Department" placeholder="">
                                <el-option label="选项一" value="shanghai" />
                                <el-option label="选项二" value="beijing" />
                            </el-select>
                        </el-col>
                    </el-form-item> -->

                    <el-button class="login_btn" type="primary" @click="submitForm(ruleFormRef)">
                        登录
                    </el-button>

                </el-form>
            </div>
        </div>
    </div>
</template>

<style lang='scss' scoped>
#login {
    width: 100%;
    min-height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;

    .login_box {
        width: 980px;
        height: 520px;
        border-radius: 30px;
        overflow: hidden;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        display: flex;

        .left {
            width: 480px;
            background-color: #466cfa;
        }

        .right {
            width: 500px;
            padding: 40px 65px;

            .login_title {
                font-size: 24px;
                border-left: 6px solid #466cfa;
                padding-left: 15px;
                margin-bottom: 32px;
                line-height: 24px;
            }

            .el-form-item {
                margin-top: 8px;
                border-radius: 5px;
            }

            .login_btn {
                margin-top: 24px;
                width: 100%;
            }
        }
    }
}
</style>
